package com.huang.auth.controller;

import com.huang.auth.converter.AuthResourceRequestTOAuthResourcePO;
import com.huang.auth.entity.request.AuthMenuAddRequest;
import com.huang.auth.entity.request.AuthMenuEditRequest;
import com.huang.auth.entity.request.AuthResourceAddRequest;
import com.huang.auth.entity.request.AuthResourceEditRequest;
import com.huang.auth.entity.vo.AuthMenuSimpleVO;
import com.huang.auth.entity.vo.AuthMenuVO;
import com.huang.auth.entity.vo.AuthResourceSimpleVO;
import com.huang.auth.service.AuthMenuService;
import com.huang.auth.service.AuthResourceService;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author HuangShen
 * @Description
 * @create 2021-11-13 17:02
 */
@RestController
@Api(tags = "菜单管理")
@RequestMapping("/backend")
public class AuthMenuController {


    @Resource
    private AuthMenuService authMenuService;
    @Resource
    private AuthResourceService authResourceService;
    @Resource
    private AuthResourceRequestTOAuthResourcePO authResourceRequestTOAuthResourcePO;

    @ApiOperation(value = "获取菜单列表")
    @GetMapping("/menus")
    public HStringResult<List<AuthMenuVO>> menuList(@RequestParam String parentId) {
        String id = StringUtils.equals(parentId, "0") ? "0" : parentId.substring(0, parentId.indexOf("$"));
        List<AuthMenuVO> authMenuVOS = this.authMenuService.menuList(Long.valueOf(id));
        return ResultBuilder.successHSingle(authMenuVOS);
    }


    // 获取简单菜单列表 用于在角色授权是使用
    @ApiOperation(value = "获取简单菜单列表")
    @GetMapping("/menus/simple")
    public HStringResult<List<AuthMenuSimpleVO>> menuSimpleList() {
        List<AuthMenuSimpleVO> authMenuSimpleVOS = this.authMenuService.menuSimpleListV2();
        return ResultBuilder.successHSingle(authMenuSimpleVOS);
    }


    /**
     * 更新菜单 属性 以及对资源的绑定
     */
    @ApiOperation(value = "更新菜单")
    @PutMapping("/menu")
    public HStringResult<String> updateMenu(@RequestHeader("userId") String operator, @RequestBody AuthMenuEditRequest authMenuEditRequest) {
        authMenuEditRequest.setUpdateUser(operator);
        if (this.authMenuService.editMenu(authMenuEditRequest)) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    /**
     * 添加菜单时选择未绑定的资源 绑定到菜单上
     */
    @ApiOperation(value = "添加菜单")
    @PostMapping("/menu")
    public HStringResult<String> addMenu(@RequestHeader("userId") String operator, @RequestBody AuthMenuAddRequest authMenuAddRequest) {
        authMenuAddRequest.setCreateUser(operator);
        // 1 创建新的菜单
        if (this.authMenuService.addMenu(authMenuAddRequest)) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    /**
     * 根据角色id 获取授权菜单权限与列表
     */
    @ApiOperation(value = "获取授权菜单权限与列表")
    @GetMapping("/menu/authorized")
    public HStringResult<List<List<String>>> authorizedMenuAndIds(@RequestParam String roleId) {
        List<List<String>> authMenuResourceIds = this.authMenuService.authorizedMenuAndIds(Long.parseLong(roleId));

        return ResultBuilder.successHSingle(authMenuResourceIds);
    }


    @ApiOperation("添加资源")
    @PostMapping("/menu/resource")
    public HStringResult<String> resourceAdd(@RequestHeader("userId") String operator, @RequestBody AuthResourceAddRequest authResourceAddRequest) {
        authResourceAddRequest.setCreateUser(operator);

        if (authResourceService.save(authResourceRequestTOAuthResourcePO.doConvert(authResourceAddRequest))) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    @ApiOperation("删除资源")
    @DeleteMapping("/menu/resource")
    public HStringResult<String> resourceDel(@RequestHeader("userId") String operator, @RequestParam("resourceId") String resourceId) {

        if (authResourceService.resourceDel(Long.parseLong(resourceId))) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    @ApiOperation("修改资源")
    @PutMapping("/menu/resource")
    public HStringResult<String> resourceEdit(@RequestHeader("userId") String operator, @RequestBody AuthResourceEditRequest authResourceEditRequest) {
        authResourceEditRequest.setUpdateUser(operator);
        if (this.authResourceService.resourceEdit(authResourceEditRequest)) {
            return ResultBuilder.successHSingle();
        }
        return ResultBuilder.failHSingle();
    }

    @ApiOperation("获取未绑定菜单的资源")
    @GetMapping("/menu/resource/simple")
    public HStringResult<List<AuthResourceSimpleVO>> menuResourceSimpleList(@RequestParam(required = false) String menuId) {
        List<AuthResourceSimpleVO> resourceSimpleVOS = authResourceService.menuResourceSimpleList(menuId);
        return ResultBuilder.successHSingle(resourceSimpleVOS);
    }


}
